'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;
    const ConnectionHistory = app.model.define(
      'connectionHistory',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
          device_id: INTEGER(11),
          member_id: INTEGER(11),
          type: INTEGER(11), // 0 断开 1 连接
          enable_id: INTEGER(11),
          enable_status: INTEGER(11), // 0 关闭 1 开启
          // 格式化时间
          updated_time: {
              type: DATE,
              get() {
                  return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          create_time: {
              type: DATE,
              get() {
                  return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          }
      },
      {
          tableName: 'connection_history',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
    );
    // 关联
    ConnectionHistory.associate = function() {
        app.model.ConnectionHistory.belongsTo(app.model.Device, { foreignKey: 'device_id', targetKey: 'id' });
        app.model.ConnectionHistory.belongsTo(app.model.Member, { foreignKey: 'member_id', targetKey: 'id' });
    }
    return ConnectionHistory
};
